import $ from 'jquery'
import eleData from './RichElement.js'
import eleOper from './ElementOperator.js'
import {pinyin} from 'pinyin-pro'

const chineseWord = {
	enter (e, node) {
		let tag = $(node).attr('tag');
		if (tag != eleData.chineseWord.pingyinId && tag != eleData.chineseWord.contentId) return;
		let text = $(node).text();
		if (text.length == 0) {
			let p = eleOper.createElementNoParent(eleData.content);
			$(node).parent().parent().after(p);
			eleOper.updateRange(p);
			e.stopPropagation();
			e.preventDefault();
			$(node).parent().remove();
			return;
		}
		chineseWord.insertNode($(node).parent().parent(), false);
		e.stopPropagation();
		e.preventDefault();
	},
	insertNode(node, isParent=false) {
		// 这个是插入中文拼音节点的数据信息
		let parent = eleOper.createElementNoParent(eleData.chineseWord);
		$(parent).empty();
		let word = $(eleData.chineseWord.word);
		$(parent).append(word);
		let pinyin = $(eleData.chineseWord.pingyin);
		$(pinyin).append($("<br/>"));
		$(word).append(pinyin);
		let con = $(eleData.chineseWord.content);
		$(con).append($("<br/>"));
		$(word).append(con);
		if (isParent) {
			$(node).append(parent);
		} else {
			$(node).after(parent);
		}
		eleOper.updateRange(con.get(0));
	},
	insertCon() {
		let node = eleOper.getSelectNode();
		if ($(node).attr('tag') == eleData.chineseWord.id) {
			// 这种处理方式
			
		} else if ($(node).attr('tag') == eleData.chineseWord.wordId) {
			
		} else if ($(node).attr('tag') == eleData.chineseWord.contentId) {
			// 拼音中的内容
			let text = $(node).text();
			if (text.length <= 0) {
				return;
			}
			$(node).text(text[0]);
			let py = pinyin(text[0])
			if (py != text[0]) {
				$(node).prev().text(py);
			}
			let focus = node;
			for(let i = 1; i < text.length; i++) {
				let word = $(eleData.chineseWord.word);
				let pinyin_w = $(eleData.chineseWord.pingyin);
				$(pinyin_w).append($("<br/>"));
				$(word).append(pinyin_w);
				let con = $(eleData.chineseWord.content);
				$(con).append($("<br/>"));
				$(con).text(text[i]);
				$(word).append(con);
				let py = pinyin(text[i])
				if (py != text[i]) {
					$(pinyin_w).text(py);
				}
				$(node).parent().after(word);
				node = con;
			}
			eleOper.updateRange(node.get(0));
		}
	},
	backspace(e, node) {
		let tag = $(node).attr('tag');
		if (tag != eleData.chineseWord.pingyinId && tag != eleData.chineseWord.contentId) return;
		let text = $(node).text();
		if (text.length == 0) {
			let siblin = chineseWord.getFocusNode(node);
			let parent = $(node).parent().parent();
			$(node).parent().remove();
			if ($(parent).children().length == 0) $(parent).remove();
			console.log(siblin)
			e.stopPropagation();
			e.preventDefault();
			// eleOper.updateRange(siblin)
		}
	},
	getFocusNode(node) {
		let siblin = $(node).parent().next();
		if (!siblin) siblin = $(node).parent().prev();
		if (siblin) return siblin.get(0);
		let n = $(node).parent().parent();
		let tag = $(n).attr('tag');
		if (tag != eleData.chineseWord.id) return null;
		siblin = $($(n).children()[0]).children()[0];
		return siblin.get(0);
	}
}

export default chineseWord;